package com.furnitur.modules.admin.service.product.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.furnitur.core.entity.product.FurnitureCategory;
import com.furnitur.core.mapper.product.FurnitureCategoryMapper;
import com.furnitur.modules.admin.service.product.FurnitureCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class FurnitureCategoryServiceImpl implements FurnitureCategoryService {

    private static final Logger log = LoggerFactory.getLogger(FurnitureCategoryServiceImpl.class);

    @Autowired
    private FurnitureCategoryMapper categoryMapper;

    @Override
    public Page<FurnitureCategory> getList(int page, int size, String name) {
        log.info("Getting category list - page: {}, size: {}, name: {}", page, size, name);
        
        LambdaQueryWrapper<FurnitureCategory> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(name)) {
            wrapper.like(FurnitureCategory::getName, name);
        }
        wrapper.orderByAsc(FurnitureCategory::getSort);
        
        Page<FurnitureCategory> result = categoryMapper.selectPage(new Page<>(page, size), wrapper);
        log.info("Found {} categories, total: {}", result.getRecords().size(), result.getTotal());
        return result;
    }

    @Override
    public FurnitureCategory create(FurnitureCategory category) {
        category.setCreatedAt(LocalDateTime.now());
        category.setUpdatedAt(LocalDateTime.now());
        categoryMapper.insert(category);
        return category;
    }

    @Override
    public FurnitureCategory update(FurnitureCategory category) {
        category.setUpdatedAt(LocalDateTime.now());
        categoryMapper.updateById(category);
        return category;
    }

    @Override
    public void delete(Long id) {
        categoryMapper.deleteById(id);
    }

    @Override
    public List<FurnitureCategory> getAllCategories() {
        log.info("Getting all categories");
        LambdaQueryWrapper<FurnitureCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(FurnitureCategory::getSort);
        List<FurnitureCategory> categories = categoryMapper.selectList(wrapper);
        log.info("Found {} categories", categories.size());
        return categories;
    }
} 